package game.editer;

import game.editer.history.ChangeAttr;
import game.data.FrameData;
import game.worlds.GameEffect;
import game.worlds.GameRole;
import hx.events.MouseEvent;
import hx.display.Graphics;

/**
 * 移动工具
 */
class ArrowMoveTools extends ObjectSelectTools {
	public static var ALPHA = 0.8;

	var moveX = new Graphics();
	var moveY = new Graphics();
	var move = new Graphics();

	override function onInit() {
		super.onInit();
		var size = 4;
		var width = 40;
		moveY.beginFill(0xff0000);
		moveY.alpha = ALPHA;
		moveY.drawRect(-size * 0.5, -width, size, width);
		moveY.drawTriangles([-size * 2, -width, size * 2, -width, 0, -width - size * 7], [0, 1, 2], [0, 0, 0]);
		moveY.y = -15;
		this.addChild(moveY);

		moveX.beginFill(0x00ff00);
		moveX.alpha = ALPHA;
		moveX.drawRect(0, -size * 0.5, width, size);
		moveX.drawTriangles([width, -size * 2, width, size * 2, width + size * 7, 0], [0, 1, 2], [0, 0, 0]);
		moveX.x = 15;
		this.addChild(moveX);

		move.beginFill(0x0000ff);
		move.alpha = ALPHA;
		move.drawRect(-size * 2, -size * 2, size * 4, size * 4);
		this.addChild(move);

		this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseOver);
	}

	private function onMouseOver(e:MouseEvent):Void {
		var g:Graphics = e.target;
		g.alpha = 1;
	}

	private function onMouseOut(e:MouseEvent):Void {
		if (isBegin)
			return;
		var g:Graphics = e.target;
		g.alpha = ALPHA;
	}

	private var __displayX:Float = 0;

	private var __displayY:Float = 0;

	private var __dataX:Float = 0;

	private var __dataY:Float = 0;

	override function onBeginMouse(x:Float, y:Float) {
		super.onBeginMouse(x, y);
		__displayX = EditerStage.objectSelect.selectedDisplay.x;
		__displayY = EditerStage.objectSelect.selectedDisplay.y;
		if (EditerStage.objectSelect.selectedDisplay is GameRole) {
			if (EditerStage.timeline.currentFrameData == null) {
				return;
			}
			__dataX = EditerStage.timeline.currentFrameData.originMoveX;
			__dataY = EditerStage.timeline.currentFrameData.originMoveY;
		} else if (EditerStage.objectSelect.selectedDisplay is GameEffect) {
			var effect:GameEffect = cast EditerStage.objectSelect.selectedDisplay;
			#if editer
			__dataX = effect.originData.x;
			__dataY = effect.originData.y;
			#end
		}
	}

	public function updateData(xEnable:Bool, yEnable, x:Float, y:Float):Void {
		EditerStage.objectSelect.update();
		if (EditerStage.timeline.currentActionData != null) {
			if (EditerStage.objectSelect.selectedDisplay is GameRole) {
				// 编辑角色的处理
				if (xEnable)
					EditerStage.timeline.currentFrameData.originMoveX = __dataX + (x - this.beginX);
				if (yEnable)
					EditerStage.timeline.currentFrameData.originMoveY = __dataY + (y - this.beginY);
			} else if (EditerStage.objectSelect.selectedDisplay is GameEffect) {
				var effect:GameEffect = cast EditerStage.objectSelect.selectedDisplay;
				#if editer
				if (xEnable)
					effect.originData.x = __dataX + (x - this.beginX);
				if (yEnable)
					effect.originData.y = __dataY + (y - this.beginY);
				#end
			}
		}
		EditerStage.attribute.updateAttrs();
	}

	override function onMoveMouse(x:Float, y:Float) {
		super.onMoveMouse(x, y);
		if (moveX.alpha == 1) {
			EditerStage.objectSelect.selectedDisplay.x = __displayX + (x - this.beginX);
			updateData(true, false, x, y);
		} else if (moveY.alpha == 1) {
			EditerStage.objectSelect.selectedDisplay.y = __displayY + (y - this.beginY);
			updateData(false, true, x, y);
		} else if (move.alpha == 1) {
			EditerStage.objectSelect.selectedDisplay.x = __displayX + (x - this.beginX);
			EditerStage.objectSelect.selectedDisplay.y = __displayY + (y - this.beginY);
			updateData(true, true, x, y);
		}
	}

	override function onEndMouse(x:Float, y:Float) {
		super.onEndMouse(x, y);
		move.alpha = moveX.alpha = moveY.alpha = ALPHA;
		EditerStage.attribute.updateActionData();
		// 记录历史记录
		if (EditerStage.timeline.currentFrameData != null) {
			var oldData = {
				originMoveX: __dataX,
				originMoveY: __dataY
			};
			var newData = {
				originMoveX: EditerStage.timeline.currentFrameData.originMoveX,
				originMoveY: EditerStage.timeline.currentFrameData.originMoveY
			}
			EditerStage.historyData.todo(new ChangeAttr(EditerStage.timeline.currentFrameData, oldData, newData));
		}
	}
}
